package com.manage.function.service.impl;

import com.manage.common.core.domain.entity.SysUser;
import com.manage.common.utils.SecurityUtils;
import com.manage.function.domain.Review;
import com.manage.function.mapper.ReviewMapper;
import com.manage.function.service.IReviewService;
import com.manage.system.domain.SysUserRole;
import com.manage.system.mapper.SysUserMapper;
import com.manage.system.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审核Service业务层处理
 *
 * @author jinghj
 * @date 2025-03-05
 */
@Service
public class ReviewServiceImpl implements IReviewService
{
    @Resource
    private ReviewMapper reviewMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;





    /**
     * 查询审核
     *
     * @param id 审核主键
     * @return 审核
     */
    @Override
    public Review selectReviewById(Long id)
    {
        return reviewMapper.selectReviewById(id);
    }


    //查询用户角色id，前端根据用户角色id做权限控制。
    @Override
    public int gerUserRoleId() {
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        int i = sysUserRole.getRoleId().intValue();
        return i;
    }

    /**
     * 查询审核列表
     *
     * @param review 审核
     * @return 审核
     */
    @Override
    public List<Review> selectReviewList(Review review)
    {
        List<Review> reviews=null;
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        if (sysUserRole.getRoleId()==1){
            reviews = reviewMapper.selectReviewList(review);
        }else {
            review.setUsername(username);
            reviews = reviewMapper.selectReviewList(review);
        }

        return reviews;
    }

    /**
     * 新增审核
     *
     * @param review 审核
     * @return 结果
     */
    @Override
    public int insertReview(Review review)
    {
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        review.setRoleId(sysUserRole.getRoleId());
        review.setStatus("2");
        review.setUsername(SecurityUtils.getUsername());
        return reviewMapper.insertReview(review);
    }



    /**
     * 修改审核
     *
     * @param review 审核
     * @return 结果
     */
    @Override
    public int updateReview(Review review)
    {
        return reviewMapper.updateReview(review);
    }

    /**
     * 批量删除审核
     *
     * @param ids 需要删除的审核主键
     * @return 结果
     */
    @Override
    public int deleteReviewByIds(Long[] ids)
    {
        return reviewMapper.deleteReviewByIds(ids);
    }

    /**
     * 删除审核信息
     *
     * @param id 审核主键
     * @return 结果
     */
    @Override
    public int deleteReviewById(Long id)
    {
        return reviewMapper.deleteReviewById(id);
    }
}
